import tushare as ts
import numpy as np
import pandas as pd
import h5py  # 导入工具包
import tables
# #生成9000,0000条数据，9千万条
# a = np.random.standard_normal((90000000, 4))
# b = pd.DataFrame(a)
# #普通格式存储：
# h5 = pd.HDFStore('test_s.h5','w')
# h5['data'] = b
# h5.close()
#
# #压缩格式存储
# h5 = pd.HDFStore('test_c4.h5', 'w', complevel=4, complib='blosc')
# h5['data'] = b
# h5.close()


# # HDF5的写入：
# imgData = np.zeros((30, 3, 128, 256))
# f = h5py.File('HDF5_FILE.h5', 'w')  # 创建一个h5文件，文件指针是f
# f['data'] = imgData  # 将数据写入文件的主键data下面
# f['labels'] = range(100)  # 将数据写入文件的主键labels下面
# f.close()  # 关闭文件
#
# # HDF5的读取：
# f = h5py.File('HDF5_FILE.h5', 'r')  # 打开h5文件
# f.keys()  # 可以查看所有的主键
# a = f['data'][:]  # 取出主键为data的所有的键值
# f.close()
#
# print(a)

# dates = pd.DataFrame({
#     'a': pd.Timestamp('20130102'),
#     'b': pd.Series(1, index=list(range(4)), dtype='float32'),
#     'c': np.array([3] * 4, dtype='int32'),
#     'd': pd.Categorical(['test', 'train', 'test', 'train']),
#     'e': 'str'
# })
#
# print(dates.dtypes)
#
# rng = pd.date_range('20150708', periods=10, freq='S')
#
# ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng)
#
# ts.resample('5Min').ohlc()

# print(ts)
#
# df0 = ts.get_k_data(code='600112')
# df1 = ts.get_k_data(code='600848')

# h5w = pd.HDFStore('test_s.h5', 'r+')
# h5w['stock_600112'] = df0
# # h5w['stock_600848'] = df1
# h5w.close()


# h5 = pd.HDFStore('test_s.h5', 'r')
# print(h5['stock_1d_603758'])
# # for h in h5:
# #     print(h)
# h5.close()


h5f = tables.open_file('database_name.h5', 'w')


# 定义schema
class ParticleDescription(tables.IsDescription):
    name = tables.StringCol(10, pos=1)
    x = tables.FloatCol(pos=2)
    y = tables.FloatCol(pos=3)
    temperature = tables.FloatCol(pos=4)

# 创建表
tbl = h5f.create_table('/', 'table_name', description_name=ParticleDescription)
# 创建索引
tbl.cols.colum_name.create_index()
# 删除索引
tbl.cols.colum_name.remove_index()

# 更改表名
tbl.rename('new_name')
h5f.rename_node('/', name='old_name', newname='new_name')

# 删除表
h5f.remove_node('/', 'table_name')

# 插入行
row = tbl.row
while some_condition:
    tbl.row['column_name1'] = value1
    tbl.row['column_name2'] = value2
    row.append()

rows = [
    ('foo', 0.0, 0.0, 150.0),
    ('bar', 0.5, 0.0, 100.0),
    ('foo', 1.0, 1.0,  25.0)
]
tbl.append(rows)

# 更新行
for row in tbl:
    row['column_name1'] = expression1
    row['column_name2'] = expression2
    row.update()

# Using a NumPy container.

rows = np.rec.array(rows)
tbl.append(rows)


tbl.flush()  # flush data in the table
# 关闭表句柄
tbl.close()

h5f.flush()  # flush all pending data
# 断开数据库连接
h5f.close()